package course.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Iterator;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.JTextComponent;

import course.controller.CourseControllerListener;
import course.po.Teacher;

public class TeacherJPanel extends JPanel {
	private JTextField searchTextField;
	private static JScrollPane tablePanel;
	private static JPanel panel;
	public static int isbn = -1;
	public static JComboBox<String> comboBox;
	private static CourseControllerListener controllerListener;
<<<<<<< HEAD
	public static int teaId;
	public TeacherJPanel() {
		this.controllerListener = new CourseControllerListener();
		this.controllerListener.setTeachertPanel(this);

		setLayout(null);

		panel = new JPanel();
		panel.setBounds(15, 195, 1020, 387);
		panel.setBorder(new LineBorder(new Color(127, 157, 185), 1, false));

		panel.setLayout(new BorderLayout(0, 0));
		panel.add(this.getTeacherTablePanel(controllerListener.getCourseService().findAllTeacher()));
		add(panel);
		JPanel paneO = new JPanel();
		paneO.setBounds(15, 15, 1020, 150);
		paneO.setBorder(new LineBorder(new Color(127, 157, 185), 1, false));
		add(paneO);
		paneO.setLayout(null);

		JLabel searchLabel = new JLabel("搜索类型：");
		searchLabel.setFont(new Font("宋体", Font.PLAIN, 24));
		searchLabel.setBounds(97, 56, 126, 40);
		paneO.add(searchLabel);

		comboBox = new JComboBox<String>();// 创建一个下拉列表框comboBox
		comboBox.addItem("教师编号");
		comboBox.addItem("教师姓名");
		comboBox.setBounds(238, 62, 155, 33);
		paneO.add(comboBox);

		searchTextField = new JTextField();
		searchTextField.setBounds(461, 62, 245, 33);
		paneO.add(searchTextField);
		searchTextField.setColumns(10);

		JButton searchTeacherButton = new JButton("查询");
		ImageIcon searchTeacherIcon = new ImageIcon(TeacherJPanel.class.getResource("/searchDean.png"));
		searchTeacherButton.setIcon(searchTeacherIcon);
		searchTeacherButton.setBounds(779, 58, 106, 40);
		paneO.add(searchTeacherButton);
		searchTeacherButton.setFont(new Font("宋体", Font.PLAIN, 19));
		searchTeacherButton.addActionListener(controllerListener);
		searchTeacherButton.setActionCommand("searchTeacherDialog");

		JPanel paneT = new JPanel();
		paneT.setBounds(15, 610, 1020, 86);
		add(paneT);
		paneT.setLayout(null);

		JButton updateTeacherButton = new JButton("修改");
		ImageIcon updateTeacherIcon = new ImageIcon(TeacherJPanel.class.getResource("/updateDean.png"));
		updateTeacherButton.setIcon(updateTeacherIcon);
		updateTeacherButton.setBounds(442, 15, 106, 40);
		paneT.add(updateTeacherButton);
		updateTeacherButton.setFont(new Font("宋体", Font.PLAIN, 19));
		updateTeacherButton.addActionListener(controllerListener);
		updateTeacherButton.setActionCommand("updateTeacherDialog");

		JButton deleteTeacherButton = new JButton("删除");
		ImageIcon deleteTeacherIcon = new ImageIcon(TeacherJPanel.class.getResource("/deleteDean.png"));
		deleteTeacherButton.setIcon(deleteTeacherIcon);
		deleteTeacherButton.setBounds(716, 15, 106, 40);
		paneT.add(deleteTeacherButton);
		deleteTeacherButton.setFont(new Font("宋体", Font.PLAIN, 19));
		deleteTeacherButton.addActionListener(controllerListener);
		deleteTeacherButton.setActionCommand("deleteTeacherDialog");

		JButton addTeacherButton = new JButton("添加");
		ImageIcon addTeacherIcon = new ImageIcon(TeacherJPanel.class.getResource("/addDean.png"));
		addTeacherButton.setIcon(addTeacherIcon);
		addTeacherButton.setBounds(156, 15, 106, 40);
		paneT.add(addTeacherButton);
		addTeacherButton.setFont(new Font("宋体", Font.PLAIN, 19));
		addTeacherButton.addActionListener(controllerListener);
		addTeacherButton.setActionCommand("addTeacherDialog");

	}

	public static JScrollPane getTeacherTablePanel(List<Teacher> teacherList) {
		String titles[] = { "编号", "教职工号", "姓名", "性别", "年龄", "所教科目编号", "所教科目名称" };
		Object[][] data = new Object[teacherList.size()][7];
		Iterator<Teacher> iter = teacherList.iterator();
		int i = 0;
		while (iter.hasNext()) {
			Teacher teacher = iter.next();
			data[i][0] = teacher.getTeaId();
			data[i][1] = teacher.getTeaNo();
			data[i][2] = teacher.getTeaName();
			data[i][3] = teacher.getTeaSex();
			data[i][4] = teacher.getTeaAge();
			data[i][5] = teacher.getCourse().getCouId();
			data[i][6] = teacher.getCourse().getCouName();
			i++;
		}
		DefaultTableModel tableModel = new DefaultTableModel(data, titles);
		JTable table = new JTable(tableModel);
		table.getTableHeader().setBackground(Color.orange); // 表头颜色
		table.getTableHeader().setFont(new Font("微软雅黑", Font.BOLD, 17)); // 表头字体
		table.setFont(new Font("微软雅黑", Font.PLAIN, 25)); // 表体字体
		table.setBackground(Color.pink); // 表体颜色
		table.getTableHeader().setPreferredSize(new Dimension(1, 30)); // 表头高度
		table.setRowHeight(table.getRowHeight() + 15); // 表体高度
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); // 表格只能选择一行
		table.repaint();
		table.addMouseListener(new MouseAdapter() {
			public void mouseClicked(final MouseEvent e) {
				isbn = table.getSelectedRow(); // 得到选中的行
//				teaId= Integer.valueOf(table.getValueAt(isbn, 0).toString());
=======

	public TeacherJPanel() {
		this.controllerListener = new CourseControllerListener();
		this.controllerListener.setTeachertPanel(this);

		setLayout(null);

		panel = new JPanel();
		panel.setBounds(15, 195, 1020, 387);
		panel.setBorder(new LineBorder(new Color(127, 157, 185), 1, false));

		panel.setLayout(new BorderLayout(0, 0));
		panel.add(this.getTeacherTablePanel(controllerListener.getCourseService().findAllTeacher()));
		add(panel);
		JPanel paneO = new JPanel();
		paneO.setBounds(15, 15, 1020, 150);
		paneO.setBorder(new LineBorder(new Color(127, 157, 185), 1, false));
		add(paneO);
		paneO.setLayout(null);

		JLabel searchLabel = new JLabel("搜索类型：");
		searchLabel.setFont(new Font("宋体", Font.PLAIN, 24));
		searchLabel.setBounds(97, 56, 126, 40);
		paneO.add(searchLabel);

		comboBox = new JComboBox<String>();// 创建一个下拉列表框comboBox
		comboBox.addItem("教师编号");
		comboBox.addItem("教师姓名");
		comboBox.setBounds(238, 62, 155, 33);
		paneO.add(comboBox);

		searchTextField = new JTextField();
		searchTextField.setBounds(461, 62, 245, 33);
		paneO.add(searchTextField);
		searchTextField.setColumns(10);

		JButton searchTeacherButton = new JButton("查询");
		ImageIcon searchTeacherIcon = new ImageIcon(TeacherJPanel.class.getResource("/searchDean.png"));
		searchTeacherButton.setIcon(searchTeacherIcon);
		searchTeacherButton.setBounds(779, 58, 106, 40);
		paneO.add(searchTeacherButton);
		searchTeacherButton.setFont(new Font("宋体", Font.PLAIN, 19));
		searchTeacherButton.addActionListener(controllerListener);
		searchTeacherButton.setActionCommand("searchTeacherDialog");

		JPanel paneT = new JPanel();
		paneT.setBounds(15, 610, 1020, 86);
		add(paneT);
		paneT.setLayout(null);

		JButton updateTeacherButton = new JButton("修改");
		ImageIcon updateTeacherIcon = new ImageIcon(TeacherJPanel.class.getResource("/updateDean.png"));
		updateTeacherButton.setIcon(updateTeacherIcon);
		updateTeacherButton.setBounds(442, 15, 106, 40);
		paneT.add(updateTeacherButton);
		updateTeacherButton.setFont(new Font("宋体", Font.PLAIN, 19));
		updateTeacherButton.addActionListener(controllerListener);
		updateTeacherButton.setActionCommand("updateTeacherDialog");

		JButton deleteTeacherButton = new JButton("删除");
		ImageIcon deleteTeacherIcon = new ImageIcon(TeacherJPanel.class.getResource("/deleteDean.png"));
		deleteTeacherButton.setIcon(deleteTeacherIcon);
		deleteTeacherButton.setBounds(716, 15, 106, 40);
		paneT.add(deleteTeacherButton);
		deleteTeacherButton.setFont(new Font("宋体", Font.PLAIN, 19));
		deleteTeacherButton.addActionListener(controllerListener);
		deleteTeacherButton.setActionCommand("deleteTeacherDialog");

		JButton addTeacherButton = new JButton("添加");
		ImageIcon addTeacherIcon = new ImageIcon(TeacherJPanel.class.getResource("/addDean.png"));
		addTeacherButton.setIcon(addTeacherIcon);
		addTeacherButton.setBounds(156, 15, 106, 40);
		paneT.add(addTeacherButton);
		addTeacherButton.setFont(new Font("宋体", Font.PLAIN, 19));
		addTeacherButton.addActionListener(controllerListener);
		addTeacherButton.setActionCommand("addTeacherDialog");

	}

	public static JScrollPane getTeacherTablePanel(List<Teacher> teacherList) {
		String titles[] = { "编号", "教职工号", "姓名", "性别", "年龄", "所教科目编号", "所教科目名称" };
		Object[][] data = new Object[teacherList.size()][7];
		Iterator<Teacher> iter = teacherList.iterator();
		int i = 0;
		while (iter.hasNext()) {
			Teacher teacher = iter.next();
			data[i][0] = teacher.getTeaId();
			data[i][1] = teacher.getTeaNo();
			data[i][2] = teacher.getTeaName();
			data[i][3] = teacher.getTeaSex();
			data[i][4] = teacher.getTeaAge();
			data[i][5] = teacher.getCourse().getCouId();
			data[i][6] = teacher.getCourse().getCouName();
			i++;
		}
		DefaultTableModel tableModel = new DefaultTableModel(data, titles);
		JTable table = new JTable(tableModel);
		table.getTableHeader().setBackground(Color.orange); // 表头颜色
		table.getTableHeader().setFont(new Font("微软雅黑", Font.BOLD, 17)); // 表头字体
		table.setFont(new Font("微软雅黑", Font.PLAIN, 25)); // 表体字体
		table.setBackground(Color.pink); // 表体颜色
		table.getTableHeader().setPreferredSize(new Dimension(1, 30)); // 表头高度
		table.setRowHeight(table.getRowHeight() + 15); // 表体高度
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); // 表格只能选择一行
		table.repaint();
		table.addMouseListener(new MouseAdapter() {
			public void mouseClicked(final MouseEvent e) {
				isbn = table.getSelectedRow(); // 得到选中的行
>>>>>>> refs/remotes/origin/master
			}
		});
		tablePanel = new JScrollPane(table);
		return tablePanel;
	}

	public static void refreshTeacher(List<Teacher> teacherList) {
		System.out.println("测试——当前教师数量为：" + teacherList.size());
		panel.remove(tablePanel);
		panel.add(TeacherJPanel.getTeacherTablePanel(controllerListener.getCourseService().findAllTeacher()),
				BorderLayout.CENTER);
		panel.validate();
		JOptionPane.showMessageDialog(panel, "重新加载数据成功！");
	}

	public void showTeacherSearchResult(List<Teacher> result) {
		panel.remove(tablePanel);
		panel.add(this.getTeacherTablePanel(result), BorderLayout.CENTER);
		this.validate();
		JOptionPane.showMessageDialog(this, "查询成功！");

	}

	public int getTeacherIsbn() {
		return isbn;
	}

	public CourseControllerListener getControllerListener() {
		return controllerListener;
	}

	public JTextComponent getSearchTextField() {
		return searchTextField;
	}
}
